package com.atguigu.demo.customer.service.impl;

import com.atguigu.demo.customer.bean.Customer;
import com.atguigu.demo.customer.mapper.CustomerMapper;
import com.atguigu.demo.customer.service.CustomerService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@DS("demo")
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;
    @Override
    public void saveCustomer(Customer customer) {
        System.out.println(" save customer = " + customer);
      //  customerMapper.insertCustomer(customer);
        customerMapper.insert(customer);
    }

    @Override
    public List<Customer> getCustomerList(Customer customer) {
     //   List<Customer> customers = customerMapper.selectCustomerListByName(customer.getName());
        String sql =" select * from t_customer  where 1=1";

        if(customer.getName()!=null){
            sql+=" and name like '%"+customer.getName()+"%'";
        }
        if(customer.getAge()!=null){
            sql+=" and age ="+customer.getAge();
        }
        List<Customer> customers = customerMapper.selectCustomerList(sql);

        return customers;
    }



    @Override
    public Customer getCustomer(Long id) {
       // Customer customer= customerMapper.selectCustomerById(id);
        Customer customer= customerMapper.selectById(id);
        return customer;
    }
}
